#include <iostream>
#include <memory>
#include "pthread.hpp"
#include "task.hpp"
#include "ringQueue.hpp"
#include <time.h>
#include <unistd.h>

void consumerRoutine(void *argv)
{
    RingQueue<Task> *ringQueue = static_cast<RingQueue<Task> *>(argv);
    while (true)
    {
        Task task;
        ringQueue->pop(&task);
        task();
        task.print();
        sleep(1);
    }
}

void producerRoutine(void *argv)
{
    RingQueue<Task> *ringQueue = static_cast<RingQueue<Task> *>(argv);
    const char ch[] = "+-*/";
    while (true)
    {
        int x = rand() % 10;
        int y = rand() % 10;
        char op = ch[rand() % 4];
        Task task(x, y, op);
        ringQueue->push(task);
        sleep(1);
    }
}

int main()
{
    RingQueue<Task> ringQueue;
    srand(time(nullptr));
    thread t1(1, consumerRoutine, &ringQueue);
    thread t2(2, consumerRoutine, &ringQueue);
    thread t3(3, producerRoutine, &ringQueue);
    thread t4(4, producerRoutine, &ringQueue);
    thread t5(5, producerRoutine, &ringQueue);
    t1.run();
    t2.run();
    t3.run();
    t4.run();
    t5.run();

    t1.join();
    t2.join();
    t3.join();
    t4.join();
    t5.join();

    return 0;
}